<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
	<title>评论回复</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link type="text/css" rel="stylesheet" href="../css/bootstrap.css">
	<link type="text/css" rel="stylesheet" href="/css/style.css?v=20210222">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
</head>
<body>

	<jsp:include page="/windowshoper/header.jsp"></jsp:include>
	
	<!--account-->
	<div class="account">
		<div class="container">
			<div class="register">
			    <c:if test="${!empty msg }">
					<div class="alert alert-danger">${msg }</div>
				</c:if>
					<div class="register-top-grid">
							    <h3>评论回复</h3>
								<form id="addForm"> 
									<c:forEach items="${order.itemList}" var="item">
										<div class="input">
											<span>商品详情: </span>
									        <p>${item.goodsName }(${item.price }) x ${item.amount }</p>
									        <input type="hidden" name="orderId" value="${order.id}">
										</div>
										<div class="input">
											<span>总价: </span>
											${order.total}
										</div>
										<c:forEach items="${item.evaluations}" var="evaluations">
											<div class="input">
													    <input type="hidden" id="${evaluations.goodsId}-orderId" value="${order.id}">
													    <input type="hidden" id="${evaluations.goodsId}-parentId" value="${evaluations.id}">
													    <input type="hidden" name="goodsId" value="${evaluations.goodsId}">
													    <input type="hidden" id="${evaluations.goodsId}-toUserId" value="${evaluations.creatorId}">
											</div>
											<div class="input">
												        <span>评论人:</span>
													    <p>${order.name}<p>
											</div>
											<div class="input">
													    <span>评论时间: </span> </br>
													    <span>${evaluations.createdTime}</span>
											</div>
											<div class="input">
												        <span><label style="color:red;">*</label>回复内容:</span> </br>
													    <textarea cols="100" rows="5" id="${evaluations.goodsId}-content" placeholder="请输入回复内容"></textarea>
											</div>
										</c:forEach> 
										<div class="clearfix"> </div>
									</c:forEach>
								</form>
							</div>
						<div class="register-but text-center">
						    <a class="btn btn-success" href="#" onclick="submit()">提交</a>
						   &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
						   <a class="btn btn-warning" href="${pageContext.request.contextPath }/windowshoper/order_list?status=5">退出</a>
						   <div class="clearfix"> </div>
						</div>
				    <div class="clearfix"> </div> 
			</div>
	    </div>
	</div>
	<!--//account-->

	<jsp:include page="/footer.jsp"></jsp:include>

    <script type="text/javascript">
    function submit() {
 	   var params = $("#addForm").serialize();
 	   params = decodeURIComponent(params,true);
 	   var list = params.split("&");
 	   console.log(list);
 	   var formData = [];
 	   var orderId = 0;
 	   list.forEach(function(param){
 		   var formList = param.split("=");
 		   if(formList[0]==="goodsId") {
 			   var item = {
 	    		   "id": 0,
 				   "goodsId": formList[1],
 				   "content": $("#"+formList[1]+"-content").val()? $("#"+formList[1]+"-content").val():"该用户未评论",
 	    		   "orderId": $("#"+formList[1]+"-orderId").val(),
 	    		   "parentId": $("#"+formList[1]+"-parentId").val(),
 	    		   "createdTime": '',
 	    		   "createdTime": '',
 	    		   "updatedTime": '',
 	    		   "toUserId": $("#"+formList[1]+"-toUserId").val(),
 	    		   "creatorId": 0
 			   
 			   }
 			   console.log(item);
 			   formData.push(item);
 			   
 		   }else if(formList[0]==="orderId"){
			   orderId = formList[1];
		   }
 	   })
 	   console.log(JSON.stringify(formData));
        //params = encodeURI(encodeURI(params));
 	   $.ajax({
 		    type : "POST",
 		    contentType : 'application/json',
 		    url : '<%=request.getContextPath()%>/windowshoper/order_reply_add',
 		    data: JSON.stringify({
 		    	"orderId": orderId,
 		    	"evaluations": formData
 		    }),
 		    async :false,
 		    dataType : "json",
 		    success : function(data) { 
 		    	window.location.href="<%=request.getContextPath()%>/windowshoper/order_list";
 		    },
 		    error:function(){
 		    	window.location.href="<%=request.getContextPath()%>/windowshoper/order_list";
 		    }

 		   });
    }
    </script>
	
</body>
</html>